// common放置公共的全局的文件
// config文件夹中放置配置文件
// router.js对文件中的路由进行二次封装

// 原有的在src/router.js中的路由信息要删除 

let routes = [
    {
        path: '/',
        name: 'AppLayout',
        // meta: { title: 'APP页面展示' },
        redirect: { name: 'index' },
        component: 'layout',
        children: [
            {
                path: '/index',
                name: 'index',
                meta: { title: '后台首页' },
                component: 'index/index',
            },
            {
                path: 'shop/goods/list',
                name: 'shop-goods-list',
                meta: { title: '商品列表' },
                component: 'shop/goods/list',
            },
            {
                path: 'shop/goods/create',
                name: 'shop-goods-create',
                meta: { title: '创建商品' },
                component: 'shop/goods/create',
            },
            {
                path: 'shop/goods/content',
                name: 'shop-goods-content',
                meta: { title: '修改商品详情' },
                component: 'shop/goods/content',
            },
            {
                path: 'shop/goods/banner',
                name: 'shop-goods-banner',
                meta: { title: '媒体设置' },
                component: 'shop/goods/banner',
            },
                        
            {
                path: 'shop/goods/attr',
                name: 'shop-goods-attr',
                meta: { title: '商品属性' },
                component: 'shop/goods/attr',
            },                      
              {
                path: 'shop/goods/sku',
                name: 'shop-goods-sku',
                meta: { title: '商品规格' },
                component: 'shop/goods/sku',
            },
            {
                path: 'image',
                name: 'image',
                meta: { title: '相册管理' },
                component: 'image/index',
            },
            {
                path: 'shop/category/list',
                name: 'shop-category-list',
                meta: { title: '分类管理' },
                component: 'shop/category/list',
            },
            {
                path: 'shop/sku/list',
                name: 'shop-sku-list',
                meta: { title: '商品规格管理' },
                component: 'shop/sku/list',
            }, {
                path: 'shop/type/list',
                name: 'shop-type-list',
                meta: { title: '商品类型管理' },
                component: 'shop/type/list',
            }, {
                path: 'shop/comment/list',
                name: 'shop-comment-list',
                meta: { title: '商品评论管理' },
                component: 'shop/comment/list',
            }, {
                path: 'order/invoice/list',
                name: 'order-invoice-list',
                meta: { title: '发票管理' },
                component: 'order/invoice/list',
            },
            {
                path: 'order/after-sale/list',
                name: 'order-after-sale-list',
                meta: { title: '售后服务' },
                component: 'order/after-sale/list',
            },
            {
                path: 'order/order/list',
                name: 'order-order-list',
                meta: { title: '订单列表' },
                component: 'order/order/list',
            },
            {
                path: 'user/user-list/list',
                name: 'user-user-list-list',
                meta: { title: '会员列表' },
                component: 'user/userlist/list',
            },
            {
                path: 'user/user-level/list',
                name: 'user-user-level-list',
                meta: { title: '会员等级' },
                component: 'user/user-level/list',
            },
            {
                path: 'set/base',
                name: 'set-base',
                meta: { title: '基础设置' },
                component: 'set/base/index',
            },
            {
                path: 'set/express',
                name: 'set-express',
                meta: { title: '物流设置' },
                component: 'set/express/index',
            },
            {
                path: 'set/manager',
                name: 'set-manager',
                meta: { title: '管理员设置' },
                component: 'set/manager/index',
            },
            {
                path: 'set/payment',
                name: 'set-payment',
                meta: { title: '交易设置' },
                component: 'set/payment/index',
            },
            {
                path: 'error/404',
                name: 'error-404',
                meta: { title: '404错误' },
                component: 'error/404',
            }
        ],
    },
    {
        path: '/login',
        name: 'LoginPage',
        meta: { title: '登录页面' },
        component: 'login/index',
    },
    {
        path: '*',
        name: '404',
        redirect: { name: 'index' },
    },
];

// 获取路由信息方法
let getRoutes = function () {
    // console.log('getRoutes function called'); // 调试输出
    createRoute(routes); // 生成路由的详细信息
    // console.log('Routes before return:', routes); // 调试输出
    return routes;       // 确保返回的是数组
};

// 自动生成路由
function createRoute(arr) {
    for (let i = 0; i < arr.length; i++) {
        if (!arr[i].component) continue; // 跳过没有 component 的路由

        // 去除index
        let val = getValue(arr[i].component); // 获取最后一个/的索引
        // 生成path和name
        arr[i].name = arr[i].name || val.replace(/\//g, '-'); // 将/替换为-，生成name
        arr[i].path = arr[i].path || `/${val}`; // 生成path

        // 动态导入组件
        // console.log(`Loading component: ../../views/${arr[i].component}.vue`);
        // arr[i].component = () => import(/* webpackChunkName: "[request]" */ `../../views/${arr[i].component}.vue`);

        // 静态导入
        arr[i].component = require(`../../views/${arr[i].component}.vue`).default;

        // 判断子路由
        if (arr[i].children && arr[i].children.length > 0) {
            createRoute(arr[i].children);
        }
    }
}

// 去除index(若路径的最后目录例index/index，为index，省去index)
function getValue(str) {
    // str = login/index
    // 获取最后一个/的索引
    let index = str.lastIndexOf('/');
    // 获取最有一个/后面的字符串
    let val = str.substring(index + 1, str.length);
    // 判断是否index结尾
    if (val === 'index') {
        // 若是，去除index
        str = str.substring(index, -1);
    }
    return str;
}

export default getRoutes();
